package com.cellngine.hub;

import java.io.IOException;
import java.net.ServerSocket;
import com.cellngine.Constants;
import com.cellngine.Cellngine;

public class WebServer
{
	private ServerSocket	server;
	private int				port;
	private Thread			thread;
	private boolean			running	= false;
	
	public WebServer(final int port)
	{
		//Generate the MIME types so we can determine the content type later on.
		new MIME();
		
		//Initialize the server and spawn a new thread to host it in.
		try
		{
			this.server = new ServerSocket(port);
		}
		catch (final IOException e)
		{
			Cellngine.log(e, Constants.log_type.error);
		}
		
		this.thread = new Thread(new WebServerThread(this, this.server));
	}
	
	public void startThread()
	{
		//Don't start the thread twice.
		if (this.thread.isAlive())
		{
			return;
		}
		
		this.running = true;
		this.thread.start();
	}
	
	public void stopThread()
	{
		this.running = false;
	}
	
	public boolean isRunning()
	{
		return this.running;
	}
	
	public int getPort()
	{
		return this.port;
	}
}